-- UIPropertySelectTarget
-- Created by chengb Apr/11/2015
-- 道具选择目标界面

UIPropertySelectTarget = class("UIPropertySelectTarget", function()
    return cc.Layer:create();
end);

function UIPropertySelectTarget.create(classId)
    return UIPropertySelectTarget.new(classId);
end

function UIPropertySelectTarget:ctor(classId)
    -- 初始化
    self:setName("UIPropertySelectTarget");

    local node = cc.CSLoader:createNode("layout/hero/PropertySelectTarget.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    self.classId = classId;
    local pet = ME.user:getActivePet();
    self.pet = pet;

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIPropertySelectTarget:resize()
    local node = self.node;
    local w = AlignM.frameSize.width;
    local h = AlignM.frameSize.height;

    local scaleX = w / DESIGN_WIDTH;
    local scaleY = h / DESIGN_HEIGHT;

    -- CT底部居中并缩放至合适大小
    AlignM.alignToBottomCenter(node, "CT");

    -- CT2顶部居中并缩放至合适大小
    AlignM.alignToTopCenter(node, "CT2");

    -- 蒙版需要拉伸至正常大小
    local CT = findChildByName(node, "CT");
    local CT2 = findChildByName(node, "CT2");
    local bg1 = findChildByName(node, "CT/bg1");
    local bg2 = findChildByName(node, "CT2/bg2");
    bg1:setScale(scaleX / CT:getScale(), scaleY / CT:getScale());
    bg2:setScale(scaleX / CT2:getScale(), scaleY / CT:getScale());

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 重绘
function UIPropertySelectTarget:redraw()
    -- 显示遮罩
    self:showMask();

    -- 显示英雄头像
    local headImg = findChildByName(self.node, "CT/head_icon");
    local icon = self.pet:getIcon();
    headImg:loadTexture(getHeroIconPath(icon));

    -- 提示信息
    local tipLabel = findChildByName(self.node, "CT/tip");
    TextStyleM.setTextStyle(tipLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_BROWN_YELLOW);
    tipLabel:setString(getLocStr("select_property_target"));

    -- 道具描述
    local descLabel = findChildByName(self.node, "CT2/desc");
    --local detail = ItemM.query(self.classId, "detail");
    local detail = FormulaM.invoke("CALC_SELECT_TARGET_DETIAL", self.classId);
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_BLUE);
    descLabel = applyString(descLabel, detail);

    -- 处于变身状态，创建变身状态的模型，否则创建普通模型
    local shapeId = ShapeChangeM.getShapeChangeId();
    if type(shapeId) == "number" and shapeId ~= -1 then
        -- 获取当前的出战宠物
        local pet = ME.user:getActivePet();
        local userPetId = pet.classId;

        self:createShapeChangeModel(shapeId);
    else
        -- 创建英雄模型
        self:createHeroModel();
    end
end

-- 创建变身后的英雄模型
function UIPropertySelectTarget:createShapeChangeModel(shapeId)
    local headImg   = findChildByName(self.node, "CT/head_icon");
    local modelNode = findChildByName(self.node, "CT/model_node");

    -- 查询变身信息
    local shapeInfo = ShapeChangeM.query(shapeId);

    -- 获取当前出战英雄
    local iconPath = getHeroLargeIconPath(shapeInfo["icon"]);
    local animation = shapeInfo["animation"];
    modelNode:removeAllChildren();
    createShapeChangeMode(shapeId, headImg, animation, modelNode, shapeInfo["model_scale"], iconPath);
end

-- 创建英雄模型
function UIPropertySelectTarget:createHeroModel()
    local headImg   = findChildByName(self.node, "CT/head_icon");
    local modelNode = findChildByName(self.node, "CT/model_node");

    -- 获取当前出战英雄
    local pet = ME.user:getActivePet();
    local classId = pet.classId;
    local petInfo = PetM.queryModel(pet:getModelId());

    local iconPath = getHeroIconPath(pet:getIcon());
    modelNode:removeAllChildren();
    createHeroModel(pet:getModelId(), headImg, modelNode, petInfo["model_scale"], iconPath);
end

-- 显示蒙版
function UIPropertySelectTarget:showMask()
    showTargetSelectMask(self.targetType);
end

-- 隐藏蒙版
function UIPropertySelectTarget:hideMask()
    hideTargetSelectMask();
end

-- 注册点击事件
function UIPropertySelectTarget:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIPropertySelectTarget");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

end

-- 注册事件处理回调函数
function UIPropertySelectTarget:registerEventCallback()
    EventMgr.register("UIPropertySelectTarget", event.TARGET_SELECTED, function(pos)
        print(pos);

        local openMonsterGrids = UIDungeonMgr:getCurLevel():getOpenMonsterGrids();
        for i = 1, #openMonsterGrids do
            print(openMonsterGrids[i].index)
            if openMonsterGrids[i].index == pos then
                if self.targetType ~= ATTACKER then
                    -- 判断一下能否使用
                    local ret = PropertyM.canUse(self.classId, pos);
                    if ret ~= true then
                        -- 不能使用，弹出提示
                        local name = ItemM.query(self.classId, "name");
                        if type(ret) == "string" then
                            if ret ~= nil then
                                alert(string.format(getLocStr(ret), name));
                            else
                                alert(string.format(ret, name));
                            end
                        else
                            alert(string.format(getLocStr("use_property_fail"), name));
                        end
                    else
                        -- 使用道具
                        DungeonActionM.go("use_property", pos, self.classId);

                        -- shit
                        if not PropertyM.isDelayPlay(self.classId) then
                            EventMgr.fire(event.PLAYER_MOVE, pos);
                        end
                    end
                end

                break;
            end
        end

        self:hideMask();

        -- 关闭当前界面
        UIMgr.getCurrentScene():removeFormByName("UIPropertySelectTarget");
    end);

    -- 界面析构后，需要清理下
    self:registerScriptHandler(function(ev)
        if ev == "exit" then
            self:hideMask();

            EventMgr.removeAll("UIPropertySelectTarget");

            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():showMenu();
            end

        elseif ev == "enter" then
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():hideMenu();
            end
        end
    end);
end




